/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.query.ws;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import org.niclab.vanbus.application.VanBusApplication;

import android.content.Context;
import android.util.Log;

public class GoogleDirectionWSQueryResResolver extends  BaseResponseResolver<GoogleDirectionWSQueryResResolver.QueryResult>{

	public static class QueryResult{
		private long duration; //sec
		private long distance; //meters
		
		public long getDuration() {
			return duration;
		}
		public void setDuration(long duration) {
			this.duration = duration;
		}
		public long getDistance() {
			return distance;
		}
		public void setDistance(long distance) {
			this.distance = distance;
		}
		
		
		
	}
	private static final String LOG_TAG="GoogleDirectionWSQueryResHandler";

	

	
	@Override
	protected QueryResult interpret(HttpEntity entity) {
		QueryResult result = new QueryResult();
		JsonFactory jsonFactory = VanBusApplication.getInstance().getJsonFactory();
		InputStream inputStream = null;
		try {
			inputStream = entity.getContent();
		} catch (IllegalStateException e) {
			Log.d("LOG_TAG", "HttpEntity exception");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("LOG_TAG","HttpEntity IOException");
			e.printStackTrace();
		}
		
		if(inputStream ==null)
			return null;
		
		Log.d(LOG_TAG,"handle response");
		
		try {
			
			//JsonParser parser = jsonFactory.createJsonParser(inputStream);
			ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
			HashMap<String,Object> obj=(HashMap<String, Object>) objectMapper.readValue(inputStream, Object.class);
			if(!obj.get("status").equals("OK"))
				return null;
			
			ArrayList<Object> routes = (ArrayList<Object>) obj.get("routes");
			HashMap<String, Object> route = (HashMap<String, Object>) routes.get(0);
			ArrayList<Object> legs = (ArrayList<Object>) route.get("legs");
			HashMap<String, Object> leg = (HashMap<String, Object>) legs.get(0);

			HashMap<String,Object> duration = (HashMap<String, Object>) leg.get("duration");
			Integer dur= (Integer) duration.get("value");
			//direction.setDuration(dur.longValue());
			result.setDuration(dur.longValue());
			
			HashMap<String,Object> distance = (HashMap<String, Object>) leg.get("distance");
			Integer dis= (Integer) distance.get("value");
			//direction.setDistance(dis.longValue());
			result.setDistance(dis.longValue());
				
		
			
			
			
			
		} catch (JsonParseException e) {
			Log.d("LOG_TAG", "JsonParseException");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("LOG_TAG","JSONParser IOException");
			e.printStackTrace();
		}
	
		return result;
	}
	

}
